package com.mall.user.services;

import cn.hutool.Hutool;
import cn.hutool.extra.mail.Mail;
import com.mall.commons.tool.exception.ProcessException;
import com.mall.user.IVerifyService;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.dal.entitys.Member;
import com.mall.user.dal.entitys.UserVerify;
import com.mall.user.dal.persistence.MemberMapper;
import com.mall.user.dal.persistence.UserVerifyMapper;
import com.mall.user.dto.UserVerifyRequest;
import com.mall.user.dto.UserVerifyResponse;
import com.mall.user.utils.ExceptionProcessorUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

@Slf4j
@Service
public class VerifyServiceImpl implements IVerifyService {
    @Autowired
    private UserVerifyMapper userVerifyMapper;
    @Autowired
    private MemberMapper memberMapper;

    @Transactional
    @Override
    public UserVerifyResponse userVerify(UserVerifyRequest request) {
        UserVerifyResponse response = new UserVerifyResponse();
        try {
            request.requestCheck();
            String userName = request.getUserName();
            String uuid = request.getUuid();
            UserVerify userVerify = new UserVerify();
            String verifyStatus = "Y";
            userVerify.setIsVerify(verifyStatus);
            //更新user_verify表
            Example verifyExample = new Example(UserVerify.class);
            verifyExample.createCriteria().andEqualTo("username", userName).andEqualTo("uuid", uuid);
            int updateVerify = userVerifyMapper.updateByExampleSelective(userVerify, verifyExample);
            if (updateVerify != 1) {
                log.error("尝试激活失败，username={}, uuid={}", userName, uuid);
                throw new ProcessException(SysRetCodeConstants.USER_INFOR_INVALID.getCode(), SysRetCodeConstants.USER_INFOR_INVALID.getMessage());
            }
            //更新member表
            Member member = new Member();
            member.setIsVerified(verifyStatus);
            Example memberExample = new Example(Member.class);
            memberExample.createCriteria().andEqualTo("username", userName);
            int updateMember = memberMapper.updateByExampleSelective(member, memberExample);
            if (updateMember != 1) {
                log.error("激活表更新成功，修改member表激活状态失败,回滚，username={}",userName);
                throw new ProcessException(SysRetCodeConstants.USER_INFOR_UPDATE_FAIL.getCode(), SysRetCodeConstants.USER_INFOR_UPDATE_FAIL.getMessage());
            }
            response.setCode(SysRetCodeConstants.SUCCESS.getCode());
            response.setMsg(SysRetCodeConstants.SUCCESS.getMessage());
            return response;
        } catch (Exception e) {
            log.error("VerifyServiceImpl.userVerify occur Exception :"+e);
            ExceptionProcessorUtils.wrapperHandlerException(response,e);
        }
        return response;
    }
}
